// router/index.ts
import { createRouter, createWebHistory } from 'vue-router';
import HomeView from '../views/HomeView.vue';
import ProjectsView from '../views/ProjectsView.vue';
import FlowEditorView from '../views/FlowEditorView.vue';
import ProfileView from '../views/ProfileView.vue';
import TeamView from '../views/TeamView.vue';
import LoginView from '../views/LoginView.vue';
import RegisterView from '../views/RegisterView.vue';
import ChatView from '../views/ChatView.vue';
import { useUserStore } from '../stores/userStore'; // 引入 user store

const routes = [
    { path: '/', component: HomeView },
    { path: '/flow', component: FlowEditorView },
    { path: '/chat', component: ChatView },
    { path: '/projects', component: ProjectsView },
    { path: '/profile', component: ProfileView },
    { path: '/team', component: TeamView },
    { path: '/login', component: LoginView },
    { path: '/register', component: RegisterView },
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});



// 添加路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  
  // 如果用户没有登录，访问 `/profile` 或 `/flow` 跳转到登录页面
  if ((to.path === '/profile' || to.path === '/flow' || to.path === '/projects' || to.path === '/team') && !userStore.isLoggedIn) {
    next('/login'); // 跳转到登录页面
  } else {
    next(); // 正常跳转
  }
});

export default router;
